package com.roy.sms.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.roy.sms.dao.ColumnDao;
import com.roy.sms.dao.RoleDao;
import com.roy.sms.dao.UserDao;
import com.roy.sms.model.Column;
import com.roy.sms.model.Role;
import com.roy.sms.model.User;
import com.roy.sms.util.StringUtil;
import com.roy.sms.vo.RoleVO;
import com.roy.sms.vo.UserVO;

@Service
public class RoleService {

	@Resource
	private RoleDao roleDao;
	
	@Resource
	private UserDao userDao;
	
	@Resource
	private ColumnDao columnDao;
	
	@Transactional
	public void add(RoleVO roleVO) {
		roleDao.add(roleVO.getRole());
	}

	@Transactional
	public void update(RoleVO roleVO) {
		roleDao.update(roleVO.getRole());
	}

	@Transactional
	public void deleteRoles(String ids) {
		if (!StringUtil.isEmpty(ids)) {
			String[] sid = ids.split(",");
			for (String id : sid) {
				roleDao.delete(Integer.valueOf(id));
			}
		}
	}
	
	@Transactional
	public RoleVO loadRole(int id){
		Role role = roleDao.load(id);
		return new RoleVO(role);
	}
	
	@Transactional
	public List<RoleVO> list(){
		List<Role> roles = roleDao.list();
		List<RoleVO> roleVOs = new ArrayList<RoleVO>();
		for(Role role : roles){
			roleVOs.add(new RoleVO(role));
		}
		return roleVOs;
	}
	
	@Transactional
	public List<UserVO> listRoleUsers(int roleId){
		Role role = roleDao.load(roleId);
		List<User> users = role.getUsers();
		List<UserVO> userVOs = new ArrayList<UserVO>();
		for(User user : users){
			userVOs.add(new UserVO(user));
		}
		return userVOs;
	}
	
	@Transactional
	public void addRoleUsers(String ids, int roleId){
		Role role = roleDao.load(roleId);
		List<User> users = role.getUsers();
		if (!StringUtil.isEmpty(ids)) {
			String[] sid = ids.split(",");
			for (String id : sid) {
				if(!containUser(users, id)){
					User user = userDao.load(Integer.valueOf(id));
					users.add(user);
				}
			}
		}
	}
	
	private boolean containUser(List<User> users, String id){
		for(User user : users){
			if(user.getId() == Integer.valueOf(id)){
				return true;
			}
		}
		return false;
	}
	
	private User getUser(List<User> users, String id){
		for(User user : users){
			if(user.getId() == Integer.valueOf(id)){
				return user;
			}
		}
		return null;
	}
	
	@Transactional
	public void removeRoleUsers(String ids, int roleId){
		Role role = roleDao.load(roleId);
		List<User> users = role.getUsers();
		if (!StringUtil.isEmpty(ids)) {
			String[] sid = ids.split(",");
			for (String id : sid) {
				User u = getUser(users, id);
				if(u != null){
					users.remove(u);
				}
			}
		}
	}

	@Transactional
	public List<Integer> getRoleColumnIds(int roleId){
		Role role = roleDao.load(roleId);
		List<Column> columns = role.getColumns();
		List<Integer> ids = new ArrayList<Integer>();
		for(Column c : columns){
			ids.add(c.getId());
		}
		return ids;
	}
	
	@Transactional
	public void addRoleColumns(int roleId, String ids){
		Role role = roleDao.load(roleId);
		List<Column> columns = role.getColumns();
		columns.clear();
		if (!StringUtil.isEmpty(ids)) {
			String[] sid = ids.split(",");
			for (String id : sid) {
				int intId = Integer.valueOf(id);
				if(intId > 0){
					Column column = columnDao.load(intId);
					columns.add(column);
				}
			}
		}
	}
	
}
